package com.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect  //表示是切面类
public class BusinessAspect {


    //定义一个切入点，对所有的方法有效。
    //声明公共切入点
    @Pointcut("execution(* com.dao.impl.StudentsDaoImpl.*(..))")
    public void myPointcut() {

    }

    @Before(value = "myPointcut()")
    public void checkPri() {
        System.out.println("权限的校验...");
    }

    @AfterReturning(value = "myPointcut()")
    public void logger() {
        System.out.println("日志记录...");
    }


    @AfterThrowing(value = "myPointcut()", throwing = "e")
    public void afterThrowing(Throwable e) {
        System.out.println("异常抛出通知..." + e.getMessage());
    }

    @After(value = "myPointcut()")
    public void after() {
        System.out.println("最终通知...");
    }

    //环绕通知；
    @Around(value = "myPointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Exception{
        Object obj = null;
        try {
            System.out.println("环绕内的前置通知");
            obj = joinPoint.proceed();
            System.out.println("环绕内的后置通知");
        } catch (Throwable e) {
            System.out.println("环绕内的异常抛出通知");
            e.printStackTrace();
            //这里必须要抛出新的异常，否则spring service捕获到。
            throw new Exception("环绕通知里抛出新的异常....");
        } finally {
            System.out.println("环绕内的最终通知");
        }
        return obj;
    }

}
